// @ts-ignore
import {ITopic_Enum, TopicStorage, TopicStorage_as_ITopic} from "./Topic.ts";
import {TopicGroupClass} from "./TopicGroup";
import {readonly} from "vue";

/**
 * 我希望 运行题目组 和 存储题目组 的同步的
 * 启动后立即加载所有题目
 * 那么 我们需要将题目存储 分散 , 因为不能说一个题目组上万字符,更新一个题目,就刷新整个题目组吧
 *    原来: key : { 题目组:题目[] ,... }
 *    现在:
 *       特殊key = "__topic_group_message__" //用来保存题目的基础信息
 *       key = <题目组名>index : topic_storage
 */

export type topic_storage = {
	topic_storage:TopicStorage //存储类型
	itopic_enum:ITopic_Enum //类型标识
};

export type __topic_group_message__ = {
	/// [0,topic_index_max] 中间可能存在 undefined
	topic_index_max:{ //记录 最大题目序号
		[topic_group_name : string]:number
	}
};
function __topic_group_message__default():__topic_group_message__ {
	return {
		topic_index_max:{}
	}
}

type topic_groups_storage = {
	[topic_group_name : string] : topic_storage[] //需要注意的是,这个数组可能是 离散的 ,即 [0,2] 缺[1]
}

/**
 * class可以有效降低心智负担
 */
class TopicGroupStorageClass {
	private ___topic_group_message__:__topic_group_message__ ;
	private _topic_groups_storage:topic_groups_storage = {};
	constructor() {// new 则加载所有数据 ,随后所有操作都与 存储同步
		this.___topic_group_message__ = this.init__topic_group_message__();
		this._topic_groups_storage = this.init_topic_groups_storage();
	}
	private init__topic_group_message__():__topic_group_message__ {
		let a = localStorage.getItem("__topic_group_message__");
		if (a == null) {
			let b =  __topic_group_message__default();
			localStorage.setItem("__topic_group_message__",JSON.stringify(b));
			return b;
		}
		return JSON.parse(a);
	}
	private init_topic_groups_storage():topic_groups_storage{
		for(let name in this.___topic_group_message__.topic_index_max){
			if(this._topic_groups_storage[name]==undefined)
				this._topic_groups_storage[name]=[];//初始化一个题目组字段
			let index_max = this.___topic_group_message__.topic_index_max[name];
			for(let j = 0; j <= index_max; j++){
				let a = localStorage.getItem(this.it_as_string(name,j));
				if(a!=null){
					this._topic_groups_storage[name][j] = JSON.parse(a);//生成一个存储题目类型
				}
			}
		}
		return this._topic_groups_storage;
	}
	init_TopicGroupClass():TopicGroupClass{
		let _topic_groups = new TopicGroupClass();
		let topic_groups = _topic_groups.unsafe_get_topic_groups();
		for(let k in this._topic_groups_storage){
			if(topic_groups[k]==undefined)
				topic_groups[k]=[];
			for(let i in this._topic_groups_storage[k]){
				topic_groups[k][i] ={topic:
					TopicStorage_as_ITopic(
						this._topic_groups_storage[k][i].itopic_enum,
						this._topic_groups_storage[k][i].topic_storage)};
			}
		}
		return _topic_groups;
	}
	private updateStorage__topic_group_message__(){
		localStorage.setItem("__topic_group_message__",JSON.stringify(this.___topic_group_message__));
	}
	private it_as_string(topic_group_name:string,index:number):string{
		return `<${topic_group_name}>${index}`
	}
	private string_as_it(it:string):[string,number] | undefined{
		if( !/^<(.*)>(\d)/.test(it) )
			return undefined;
		let str = RegExp.$1;
		let number = Number(RegExp.$2);
		return [str,number]
	}
	/// 根据题目组获取题目组的题目数量
	get_topic_index_max(name:string):number|undefined{
		return this.___topic_group_message__.topic_index_max[name]
	}
	/// 获取所有题目组名
	get_topic_name_all():string[]{
		let a:string[] = [];
		for(let k in this._topic_groups_storage){
			a.push(k);
		}
		return a;
	}
	/// 题目最大序号自增
	private topic_index_max_selfAdd(name:string):number{
		let index_max = this.get_topic_index_max(name);
		if(index_max == undefined){
			this.___topic_group_message__.topic_index_max[name] = 0;
		}else {
			this.___topic_group_message__.topic_index_max[name]++;
		}
		this.updateStorage__topic_group_message__();
		return this.___topic_group_message__.topic_index_max[name];
	}
	
	
	
	/// 获取一个 存储类型题目
	get_topic_storage(name:string, index:number):topic_storage | undefined{
		if(this._topic_groups_storage[name]==undefined) return undefined;
		return this._topic_groups_storage[name][index]
	}
	/// 修改或创建一个题目 , 不管是否存在 , 直接覆盖!
	private unsafe_set_topic(topic:topic_storage){
		let name = topic.topic_storage.topics_name;
		let index = topic.topic_storage.index;
		let key = this.it_as_string(name,index);
		if(this._topic_groups_storage[name] == undefined )
			this._topic_groups_storage[name] = [];
		this._topic_groups_storage[name][index] = topic;
		localStorage.setItem(key,JSON.stringify(topic));
	}
	/// 加入一个题目 , 不管你设置的index ,优先填入 空缺序号
	add_topic(topic:topic_storage):topic_storage{
		let name = topic.topic_storage.topics_name;
		let index_max = this.get_topic_index_max(name);
		if(index_max == undefined){
			topic.topic_storage.index = this.topic_index_max_selfAdd(name);//必然返回 0
			this.unsafe_set_topic(topic);
			return topic;
		}
		for (let i=0;i<=index_max;i++){
			if( this.get_topic_storage(name,i) == undefined ){//找出缺少的题目需要,进行填补
				topic.topic_storage.index=i;
				this.unsafe_set_topic(topic);
				return topic;
			}
		}
		topic.topic_storage.index = this.topic_index_max_selfAdd(name);
		this.unsafe_set_topic(topic);
		return topic;
	}
	
	/// 删除一个题目
	remove_topic(topic_group_name:string,index:number){
		if(this._topic_groups_storage[topic_group_name]==undefined
			|| this._topic_groups_storage[topic_group_name][index]==undefined) return;
		delete this._topic_groups_storage[topic_group_name][index];
		if(index == this.___topic_group_message__.topic_index_max[topic_group_name]){
			this.___topic_group_message__.topic_index_max[topic_group_name]--;
			this.updateStorage__topic_group_message__();
		}
		localStorage.removeItem(this.it_as_string(topic_group_name,index));
	}
	
}
/// 你不应该直接操作它,你需要通过 TopicGroup 来间接操作
export const TopicGroupStorage:TopicGroupStorageClass = new TopicGroupStorageClass();
